<template>
  <work-template v-model="detailForm" @submitOn="submitOn"
                 @closeSelectedTag="closeSelectedTag" :butShow="bool">
    <div slot="form">
      <renewal-table v-model="detailForm" :state="isEdit" ref="refRenewalTable" :isDownload="true"/>
    </div>

  </work-template>
</template>

<script>
import renewalTable from "../components/renewal/renewalTable.vue";
import {submitRenewal, getRenewalDetails} from "@/api/cwgd/renewal.js";
import {getWorkDetails} from "@/api/cwgd/insureApi.js";
export default {
  name: "AddRenewal",
  components: {
    renewalTable,
  },
  computed: {
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    }
  },
  watch: {},
  data() {
    return {
      detailForm: {
        tableData: [],
        syxCarCount: 0,
        jqxCarCount: 0,
        jqxTotalPrice: 0,
        carTaxTotalPrice: 0,
        syxTotalPrice: 0,
        qtTotalPrice: 0,
        remark: '',
        policyOss: '',
      },
    };
  },
  created() {
    if (this.$route.query.type === 'edit') {
      if (typeof this.$route.query.id === 'string') {
        this.handleDetail([this.$route.query.id]);
      } else {
       this.handleDetail(this.$route.query.id);
      }
    }
    if(this.$route.query.type === 'view'){
      this.handleData(this.$route.query.id)
    }

  },
  mounted() {


  },
  methods: {
    //获取详情
    handleDetail(id) {

      getRenewalDetails(id).then((res) => {
        if (res.code === 200) {
          this.detailForm.tableData = res.data;
        }
      });
    },

    handleData(id) {
      getRenewalDetails([id]).then((res) => {
        if (res.code === 200) {
          this.detailForm.tableData = res.data;
        }
      });
    },

    //提交
    submitOn() {
      this.$refs.refRenewalTable
          .handleEffect()
          .then(() => {
            const loading = this.$loading({
              lock: true,
              text: "保存中",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)",
            });
            submitRenewal(this.detailForm)
                .then((res) => {
                  if (res.code === 200) {
                    loading.close();
                    this.closeSelectedTag()
                  }
                })
                .catch(() => {
                  loading.close();
                });
          })
          .catch(() => {
          });
    },

    //关闭
    closeSelectedTag() {
      this.$tab.closePage().then(({visitedViews}) => {
        this.$router.push({
          path: "/cwgd/renewalList",
        });
      });
    },
  },
};
</script>

<style scoped lang=""></style>


